/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.dfdchem.imd.kaiwu.manager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.dfdchem.imd.kaiwu.application.KWApp;
import com.dfdchem.imd.kaiwu.model.User;

import zuo.biao.library.util.JSON;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;

/**数据工具类
 * @author Lemon
 */
@SuppressLint("ApplySharedPref")
public class DataManager {
	private final String TAG = "DataManager";
	private final String PATH = "kaiwu_1.0.0";
	private final SharedPreferences sdf;

	private Context mContext;
	private DataManager(Context context) {
		this.mContext = context;
		sdf = mContext.getSharedPreferences(PATH, Context.MODE_PRIVATE);
	}

	@SuppressLint("StaticFieldLeak")
	private static DataManager instance;
	public static DataManager getInstance() {
		if (instance == null) {
			synchronized (DataManager.class) {
				if (instance == null) {
					instance = new DataManager(KWApp.getInstance());
				}
			}
		}
		return instance;
	}

	// 当前用户
	public final String KEY_CURRENT_USER = "KEY_CURRENT_USER";

	// 当前版本第一次启动
	public final String KEY_FIRST_LAUNCHER = "KEY_FIRST_LAUNCHER";

	// 当前版本第一次启动
	public final String KEY_X_TOKEN = "KEY_X_TOKEN";

	// 判断是内网版还是外网版，用于接口地址切换
	public final String KEY_IS_INTERNAL = "KEY_IS_INTERNAL";

	// 是否使用X5浏览器内核
	public final String KEY_WEB_VIEW_KERNEL_X5 = "KEY_WEB_VIEW_KERNEL_X5";

	// 是否是测试模式
	public final String KEY_WEB_VIEW_KERNEL_IS_TEST = "KEY_WEB_VIEW_KERNEL_IS_TEST";

	/**获取当前用户
	 * @return
	 */
	public User getUser() {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		return JSON.parseObject(sdf.getString(KEY_CURRENT_USER, null), User.class);
	}

	/**保存用户
	 * @param user
	 */
	public void saveUser(User user) {
		if (sdf == null || user == null) {
			Log.e(TAG, "saveUser sdf == null || user == null >> return;");
			return;
		}
		Log.i(TAG, "saveUser  key = user.getId() = " + user.getId());
		sdf.edit().remove(KEY_CURRENT_USER).putString(KEY_CURRENT_USER, JSON.toJSONString(user)).commit();
	}

	/**删除用户
	 * @param sdf
	 */
	public void removeUser(SharedPreferences sdf, long userId) {
		if (sdf == null) {
			Log.e(TAG, "removeUser sdf == null  >> return;");
			return;
		}
		sdf.edit().remove(StringUtil.getTrimedString(userId)).commit();
	}

	/**获取当前版本是否是第一次登录
	 * @return
	 */
	public Boolean isFirstLaunch(String version) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		return sdf.getBoolean(KEY_FIRST_LAUNCHER + version, true);
	}

	/**写入当前版本已启动信息
	 * @return
	 */
	public Boolean isLaunched(String version) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		String key = KEY_FIRST_LAUNCHER + version;
		return sdf.edit().remove(key).putBoolean(key, false).commit();
	}

	/**清除已启动信息
	 * @return
	 */
	public Boolean clearIsLaunched(String version) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		String key = KEY_FIRST_LAUNCHER + version;
		return sdf.edit().remove(key).putBoolean(key, true).commit();
	}

	/**设置是否是内网版
	 * @return
	 */
	public Boolean setIsIntranet(boolean isIntranet) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		String key = KEY_IS_INTERNAL;
		return sdf.edit().remove(key).putBoolean(key, isIntranet).commit();
	}

	/**获取是否使用X5内核浏览器
	 * @return
	 */
	public Boolean isX5() {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		return sdf.getBoolean(KEY_WEB_VIEW_KERNEL_X5, false);
	}

	/**设置是否是测试模式
	 * @return
	 */
	public Boolean setIsTest(boolean isTest) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		String key = KEY_WEB_VIEW_KERNEL_IS_TEST;
		return sdf.edit().remove(key).putBoolean(key, isTest).commit();
	}

	/**获取是否是测试模式
	 * @return
	 */
	public Boolean isTest() {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		return sdf.getBoolean(KEY_WEB_VIEW_KERNEL_IS_TEST, false);
	}

	/**设置是否使用X5内核浏览器
	 * @return
	 */
	public Boolean setIsX5(boolean isX5) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		String key = KEY_WEB_VIEW_KERNEL_X5;
		return sdf.edit().remove(key).putBoolean(key, isX5).commit();
	}

	/**获取是否是内网版
	 * @return
	 */
	public Boolean isIntranet() {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		return sdf.getBoolean(KEY_IS_INTERNAL, true);
	}

	/**获取X-Token
	 * @return
	 */
	public String getXToken() {
		if (sdf == null) {
			Log.e(TAG, "getXToken sdf == null >>  return;");
			return null;
		}
		return sdf.getString(KEY_X_TOKEN, null);
	}

	/**写入X-Token
	 * @return
	 */
	public Boolean putXToken(String token) {
		if (sdf == null) {
			Log.e(TAG, "getUser sdf == null >>  return;");
			return null;
		}
		return sdf.edit().remove(KEY_X_TOKEN).putString(KEY_X_TOKEN, token).commit();
	}
}
